package com.wgz.sbm.Interceptor;

import com.wgz.sbm.bean.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author 王宫泽
 * @date 2021/4/11
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public  boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception{
        //登陆成功
        HttpSession session=request.getSession();
        //user信息
        User user=(User) session.getAttribute("user");
        //判断
        if(user!=null){
            return true;
        }
        //给出提示信息返回登陆页面
        request.setAttribute("msg","您未登录，请先登陆");
        request.getRequestDispatcher("/").forward(request,response);
        return false;
    }
}
